//基于插件的上传组件
$.fn.ajaxUpload = function(options) {
	var fileCount = $("#"+options.inputId+"").val().split(",").length;
	var defaults = {
		url:'/servlet/jqueryUploader',
        showImgId:"upload_IdCardUp",//上传进程区域
        showImageArea:"showFileIdCardUp",//上传后显示区域
        inputId:"idCardUp",//存值表单Id
        uploadSize:1,
        basePath:"",
        sendData:{
           alUploadCount:fileCount,
           limitUploadCount:options.uploadSize
        }
	};
	var opts = $.extend(defaults, options);
	if ($(this) != null && typeof($(this)) == 'object') {
		$(this).uploadify({
			'uploader' : opts.basePath+'scripts/uploadfy/uploadify.swf',
			'script' : opts.url,
			'scriptData':opts.sendData,
			'method' : 'GET',
			'cancelImg' : opts.basePath+'scripts/uploadfy/cancel.png',
			'folder' : '/uploads',
			'buttonImg' : opts.basePath+'scripts/uploadfy/broswerFile.png',
			'wmode' : 'transparent',
			'width' : 122,
			'height' : 36,
			'queueID' : opts.showImgId,
			'auto' : true,
			'multi' : false,
			'queueSizeLimit' :3,//同时上传文件的数量
			'simUploadLimit':opts.uploadSize,
			'sizeLimit' : 5048576,//只允许上传不大于1M的图片
			'fileDesc' : 'Image Files',
			'fileExt' : '*.jpg;*.gif;*.jpeg;*.png;*.bmp',
			 onComplete : function(event, queueID, fileObj, response, data) {
				 //addPic(response,opts.showImageArea, opts.inputId);
			 	var fileCount = $("#"+options.inputId+"").val().split(",").length;
			 	if(fileCount>opts.uploadSize&&$("#"+options.inputId+"").val()!=","){
			 		$.messager.alert('我的消息','此栏只能上传['+opts.uploadSize+']个文件','error');
			 	    $.get("/servlet/jqueryUploader",{method:"remove",url:response});
			 	}else{
			 	     var showFileId = opts.showImageArea;
			 	     $("#"+showFileId).show();
					 var inputId = opts.inputId;
					 response = response.replace(/\r/ig, "").replace(/\n/ig, "");
					 var pics = $("#"+showFileId).find("A");
					 var uploadFiles = $("#"+inputId);
					 var imgWidth = inputId == '+inputId+'? " width=\"169\"": " width=\"160\"";
					 if (pics.length == 0) {
						 $("#" + showFileId).empty();
					 }
					 var smallPath = getSmaillPath(response);
					 var imgHtml = "<dd>" +
					 		 "<a class=\"fancybox-effects-a\" href=\""+response+"\" title=\"新增图片\">"+
								 		   "<img src=\""+smallPath+"\" border='none' width=\"95\" height=\"95\" /><br/>" +
								 		  "</a>"+
					 		"<a href=\"javascript:void(0);\" class=\"c5\" onClick=\"delPic(this.parentNode,'"+inputId+"');\">删除</a>" +
					 	  "</dd>";
					 $("#"+showFileId).append(imgHtml);
					 var curImageVal = uploadFiles.val();
					 if(!curImageVal.endWith(",")){
					     curImageVal=curImageVal+",";
					 }
					 var inputIdValue = curImageVal+response;
					 if(curImageVal==","){
					     inputIdValue = response;
					 }
					 uploadFiles.val(inputIdValue);
			 	}
				 
			 },
			 'onSelect' : function(e, queueId, fileObj) {
			 	//验证已上传了多少文件
				if ($("#"+opts.inputId+"").val().length>300) {
					$(this).uploadifyCancel(queueId);
					$(this).uploadifyCancel($('.uploadifyQueueItem').first().attr('id').replace($(this).attr("id"), ''));
				}
			 }
		});
	}
};

//得到小图地址
function getSmaillPath(imageUrl){
	var path = imageUrl.substring(0,imageUrl.lastIndexOf("/")+1);
	var fileName = imageUrl.substr(imageUrl.lastIndexOf("/")+1);
	var newFileName =  path+"small_"+fileName;
	return newFileName;
}

//删除上传的图片
function delPic(node, filesInputId) {
	//得到删除的imgurl
	var imgUrl = node.getElementsByTagName("IMG")[0].src;
	var startIndex = imgUrl.indexOf("productsImg");
	var endIndex = imgUrl.length;
	imgUrl = imgUrl.substring(startIndex,endIndex);
	imgUrl = imgUrl.replace("small_","");
	imgUrl = imgUrl.substring(imgUrl.lastIndexOf("/"),imgUrl.length);
	//alert(imgUrl);
	//得出现有的img值
	var uploadFiles = $("#"+filesInputId);
	var uploadFilesValue = uploadFiles.val();
	var curVal = uploadFilesValue.replace('\r', '').replace('\n', '').replace(',,', ',');
	//去除删除的img值
	var curValArr = curVal.split(",");
	for (var i = 0; i < curValArr.length; i++) {
		//console.log(curValArr[i]);
		//console.log(imgUrl);
		//alert(curValArr[i]);
		if(curValArr[i].indexOf(imgUrl)>-1){
			curVal = curVal.replace(curValArr[i],"");
			break;
		}
	}
	//当串只有一逗号直接设为空
	if(curVal==","){
	   curVal = "";
	}
	//当串开头为一逗号直接设为空
	if(curVal.indexOf(",")==0){
	   curVal = curVal.substring(1);
	}
	//alert("去除逗号之前："+curVal);
	//当结尾为逗号，则去除
	if(curVal.endWith(",")){
	   curVal = curVal.substring(0,curVal.length-1);
	}
	
	if(curVal.startWith(",")){
	   curVal = curVal.substring(1,curVal.length);
	}
	//alert("去除逗号之后："+curVal);
	uploadFiles.val(curVal);
	try {
		$.get("/servlet/jqueryUploader",{method:"remove",url:imgUrl});
	} catch (err) {
		alert(err);
	}
	node.parentNode.removeChild(node);
}

function DrawImage(ImgD) {
	var img = $(ImgD);
	if (img.width() > 0 && img.height() > 0) {
		if (img.width() > img.height()) {
			if (img.width() > 100) {
				img.width(100);
				img.height((img.height() * 100) / img.width());
			}
		} else {
			if (img.height() > 100) {
				img.height(100);
				img.width((img.width() * 100) / img.height());
			}
		}
	}
}

String.prototype.startWith=function(str){     
  var reg=new RegExp("^"+str);     
  return reg.test(this);        
}  

String.prototype.endWith=function(str){     
  var reg=new RegExp(str+"$");     
  return reg.test(this);        
}
